﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using SellAndBuy.Web.AdQueryServiceReference;
using SellAndBuy.Web.Controllers.ActionResults;
using SellAndBuy.Web.ViewModels;

namespace SellAndBuy.Web.Controllers
{
    public class HomeController : Controller
    {
        ///// <summary>
        ///// Ad managemenet service
        ///// </summary>
        //private readonly IAdQueryService _adQueryService;

        ///// <summary>
        ///// Construct Home Controller and Resolve Ad Management Service
        ///// </summary>
        ///// <param name="adQueryService">Ad Management Service instance</param>
        //public HomeController(IAdQueryService adQueryService)
        //{
        //    _adQueryService = adQueryService;
        //}



        public ActionResult Index(int? page, int? pageSize)
        {
            if (!page.HasValue || !pageSize.HasValue)
            {
                page = 0;
                pageSize = 10;
            }

            var adQueryService = new AdQueryServiceClient();
            var ads = adQueryService.FindAdsPaged(page.Value, pageSize.Value);
            return View(new AdListViewModel(ads, page.Value, pageSize.Value));
        }

        public ActionResult AdDetails(int id)
        {
            var adQueryService = new AdQueryServiceClient();
            var ad = adQueryService.FindAdByID(id);
            return View(new AdDetailsViewModel(ad));
        }

        public ActionResult AdEdit(int id)
        {
            //var ad = _adQueryService.FindAdByID(id);
            //return View(new AdEditViewModel(ad));
            return View();
        }

        [HttpPost]
        public ActionResult AdEdit(int id, FormCollection collection)
        {
            return new EmptyResult();
        }

        public ActionResult About()
        {
            return View();
        }

        //public ActionResult ShowMainPic(int adID, int maxWidth, int maxHeight)
        //{
        //    var ad = _AdQueryService.FindAdByID(adID);

        //    if (ad == null || ad.MainPicture == null)
        //        return new EmptyResult();

        //    return this.GetResizedImageResult(ad.MainPicture.FileName, ad.MainPicture.Picture, maxWidth, maxHeight);
        //}

        public ActionResult ShowPic(int adID, string fileName, int maxWidth, int maxHeight)
        {
            var adQueryService = new AdQueryServiceClient();
            var ad = adQueryService.FindAdByID(adID);
            if (ad == null)
                return new EmptyResult();

            var adPicture = ad.Pictures.FirstOrDefault(p => p.FileName == fileName);
            if (adPicture == null)
                return new EmptyResult();


            return GetResizedImageResult(adPicture.FileName, adPicture.Picture, maxWidth, maxHeight);
        }



        private ActionResult GetResizedImageResult(string fileName, byte[] image, int maxWidth, int maxHeight)
        {
            using (var ms = new MemoryStream(image))
            {
                using (var img = Image.FromStream(ms))
                {
                    var originalSize = img.Size;
                    if (originalSize.Width <= maxWidth && originalSize.Height <= maxHeight)
                    {
                        return new ImageActionResult {FileName = fileName, Image = image};
                    }
                    var widthtRatio = (double) maxWidth/originalSize.Width;
                    var heightRatio = (double) maxHeight/originalSize.Height;
                    var ratio = Math.Min(widthtRatio, heightRatio);
                    var newSize = new Size((int) (originalSize.Width*ratio),
                                           (int) (originalSize.Height*ratio));

                    using (var btm = new Bitmap(newSize.Width, newSize.Height))
                    {
                        btm.MakeTransparent(Color.White);
                        using (var gr = Graphics.FromImage(btm))
                        {
                            gr.Clear(Color.White);
                            gr.CompositingQuality = CompositingQuality.HighSpeed;
                            gr.InterpolationMode = InterpolationMode.Default;
                            gr.DrawImage(img, 0, 0, newSize.Width, newSize.Height);
                            gr.Flush();
                            using (var outputMs = new MemoryStream())
                            {
                                btm.Save(outputMs, img.RawFormat);
                                var result = new ImageActionResult
                                                 {
                                                     FileName = fileName,
                                                     Image = outputMs.ToArray()
                                                 };
                                return result;
                            }
                        }
                    }
                }
            }
        }

    }
}